﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using PSDLib;
using PSDLib.Models;
using PSDManager.SystemSettings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager
{
    public partial class FrmAddBurn : Form
    {
        private dl_task currDownloadTask;
        public BurnTask BurnTask { get; set; }
        public FrmAddBurn()
        {
            InitializeComponent();

            cmbDVDSize.SelectedIndex = 0;
            rbtnDouble.Checked = true;
            txtCopies.Value = 2;
        }

        private long GetDVDSize()
        {
            long size = 0;
            if (cmbDVDSize.SelectedIndex == 0)
            {
                //size = Convert.ToInt64(4.7 * 1024 * 1024 * 1024);
                size = Convert.ToInt64(1 * 1024 * 1024 * 100);
            }
            else if (cmbDVDSize.SelectedIndex == 1)
            {
                size = Convert.ToInt64(8.5 * 1024 * 1024 * 1024);
            }

            return size;
        }

        public void GetDownloadTaskData(dl_task task)
        {
            currDownloadTask = task;

            if (task != null)
            {
                tvwBurn.Nodes.Clear();

                BurnTask = new BurnTask();
                BurnTask.OnAddDVD += BurnTask_OnAddDVD;
                BurnTask.OnAddFile += BurnTask_OnAddFile;

                BurnTask.BurnInfo = new dl_burn()
                {
                    id = Guid.NewGuid().ToString(),
                    title = task.task_name,
                    device_id = task.download_device_id,
                    dl_task_id = task.id,
                    entry_id = task.entery_id,
                    object_id = task.object_id,
                    dvd_size = GetDVDSize(),
                    state = -1
                };

                this.txtTitle.Text = BurnTask.BurnInfo.title;

                var result_channels = HttpHelper.Get<HResult<List<dl_task_channel>>>(AppSettings.Setting.API.GetChannelList + "?taskId=" + task.id);

                if (result_channels.success)
                {
                    foreach (var ch in result_channels.data)
                    {
                        var result_files = HttpHelper.Get<HResult<List<dl_files>>>(AppSettings.Setting.API.GetChannelFiles + "?channelId=" + ch.id);

                        if (result_files.success)
                        {
                            foreach (var file in result_files.data)
                            {
                                var bf = new dl_burn_files()
                                {
                                    id = Guid.NewGuid().ToString(),
                                    burn_id = BurnTask.BurnInfo.id,
                                    file_id = file.id,
                                    file_name = file.file_name,
                                    file_path = file.file_path,
                                    dvd_file_name = file.file_name,
                                    dvd_file_path = file.channel_name,
                                    create_time = DateTime.Now,
                                    dl_task_id = file.dl_task_id,
                                    file_size = file.file_size,
                                    state = -1
                                };

                                BurnTask.AddFile(bf);
                            }
                        }
                    }
                }
            }
        }

        private void BurnTask_OnAddFile(dl_burn_files file)
        {
            var node_dvd = tvwBurn.Nodes.Find(file.dvd_id, false);

            if (node_dvd.Length > 0)
            {
                TreeNode node_file = new TreeNode();
                node_file.Text = file.file_name + " [" + PSDFunction.FormatByteSize(file.file_size) + "]";
                node_file.Name = file.id;
                node_file.ImageIndex = 2;
                node_file.SelectedImageIndex = 2;
                node_file.Tag = file;

                node_dvd[0].Nodes.Add(node_file);

                var dvd = node_dvd[0].Tag as dl_burn_dvd;
                node_dvd[0].Text = dvd.dvd_title + " [" + PSDFunction.FormatByteSize(dvd.file_total_size) + "/" + PSDFunction.FormatByteSize(dvd.dvd_size) + "]";
            }
        }

        private void BurnTask_OnAddDVD(dl_burn_dvd dvd)
        {
            TreeNode node_dvd = new TreeNode();
            node_dvd.Text = dvd.dvd_title + " [" + PSDFunction.FormatByteSize(dvd.file_total_size) + "/" + PSDFunction.FormatByteSize(dvd.dvd_size) + "]";
            node_dvd.Name = dvd.id;
            node_dvd.ImageIndex = 0;
            node_dvd.SelectedImageIndex = 0;
            node_dvd.Tag = dvd;

            tvwBurn.Nodes.Add(node_dvd);
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            BurnTask.BurnInfo.title = txtTitle.Text.Trim();
            BurnTask.BurnInfo.burn_copy = Convert.ToInt32(txtCopies.Value);
            BurnTask.BurnInfo.burn_type = (rbtnSingle.Checked ? 1 : 2);
            BurnTask.BurnInfo.burn_mode = 0;
            BurnTask.BurnInfo.burn_start_time = DateTime.Now;

            foreach (dl_burn_dvd dvd in BurnTask.DVDList)
            {
                dvd.dvd_title = BurnTask.BurnInfo.title + "-" + dvd.dvd_num;
            }

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void cmbDVDSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDVDSize.SelectedIndex >= 0)
            {
                GetDownloadTaskData(currDownloadTask);
            }
        }
    }
}
